<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">浅谈接口</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-09-22</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h2 id="一、接口概述与生活举例"><a href="#一、接口概述与生活举例" class="headerlink" title="一、接口概述与生活举例"></a>一、接口概述与生活举例</h2><p>啥是接口？不多bb，上图！</p>
<img src="/2019/09/22/interface/接口.png" title="接口">

<p>接口就是多个类的公共规范。</p>
<p>接口是一种引用数据类型，最重要的内容就是其中的：抽象方法。</p>
<h2 id="二、接口的基本格式"><a href="#二、接口的基本格式" class="headerlink" title="二、接口的基本格式"></a>二、接口的基本格式</h2><p>那么，<strong>如何定义一个接口的格式</strong>？</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> 接口名称 </span>&#123;</span><br><span class="line"><span class="comment">//接口内容</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>备注:如果换成了关键字interface之后，编译生成的字节码文件仍然是：.java –&gt; .class</p>
<p>如果是Java 7，那么接口中可以包含的内容有：</p>
<ol>
<li>常量</li>
<li>抽象方法</li>
</ol>
<p>如果是Java 8，那么接口中还可以额外包含有：</p>
<ol start="3">
<li>默认方法</li>
<li>静态方法</li>
</ol>
<p>如果是Java 9，还可以额外包含有：</p>
<ol start="5">
<li>私有方法</li>
</ol>
<p><strong>接口使用步骤</strong></p>
<ol>
<li><p>接口不能直接使用，必须有一个”实现类”来”实现”该接口。<br>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 实现类名称 <span class="keyword">implements</span> 接口名称 </span>&#123;</span><br><span class="line"><span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>接口的实现类必须覆盖重现（实现）接口中所有的<strong>抽象方法</strong>。<br><em>实现：去掉abstract关键字，加上方法体大括号</em></p>
</li>
<li><p>创建实现类对象，进行使用。</p>
</li>
</ol>
<h2 id="三、接口的抽象方法"><a href="#三、接口的抽象方法" class="headerlink" title="三、接口的抽象方法"></a>三、接口的抽象方法</h2><p>在任何版本Java中，接口都能定义抽象方法。</p>
<p>格式：</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">abstract</span> 返回值类型 方法名称(<span class="type">参数列表</span>);</span></span><br></pre></td></tr></table></figure>

<p>注意事项：</p>
<ol>
<li>接口当中的抽象方法，修饰符必须是两个固定的关键字：public abstract</li>
<li>这两个关键字修饰符，可以选择性地省略。（刚学的话，不推荐省略）</li>
<li>方法的三要素（方法名、参数列表、返回值），可以随意定义。</li>
</ol>
<p>如果实现类并没有覆盖重写接口中所有的抽象方法，那么这个实现类自己就必须是抽象类。</p>
<p>MyInterfaceAbstract.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">MyInterfaceAbstract</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这是一个抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">methodAbs1</span>(<span class="params"></span>)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这也是抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">methodAbs2</span>(<span class="params"></span>)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这也是抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs3</span>(<span class="params"></span>)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这也是抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">methodAbs4</span>(<span class="params"></span>)</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceAbstractImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceAbstractImpl</span> <span class="keyword">implements</span> <span class="title">MyInterfaceAbstract</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"这是第一个方法！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"这是第二个方法！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"这是第三个方法！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"这是第四个方法！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo01Interface.java</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01Interface</span> &#123;</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 错误写法！不能直接new接口对象使用。</span></span><br><span class="line"><span class="comment">//        MyInterfaceAbstract inter = new MyInterfaceAbstract();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建实现类的对象使用</span></span><br><span class="line">        MyInterfaceAbstractImpl impl = <span class="keyword">new</span> MyInterfaceAbstractImpl();</span><br><span class="line">        impl.methodAbs1();</span><br><span class="line">        impl.methodAbs2();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、接口的默认方法"><a href="#四、接口的默认方法" class="headerlink" title="四、接口的默认方法"></a>四、接口的默认方法</h2><p>从java 8开始，接口里允许默认方法</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">default</span> 返回值类型 方法名称(参数列表)&#123;</span><br><span class="line"><span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li>接口的默认方法，可以通过接口实现类对象，直接调用。</li>
<li>接口的默认方法，也可以被接口实现类进行覆盖重写。</li>
</ol>
<p>备注：接口当中的默认方法，可以解决接口升级的问题。</p>
<p>MyInterfaceDefault.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">MyInterfaceDefault</span> &#123;</span><br><span class="line">	<span class="comment">//抽象方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">methodAbs</span>(<span class="params"></span>)</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//新添加一个抽象方法，会导致两个实现类报错</span></span><br><span class="line">	<span class="comment">//public abstract void methodAbs2();</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//为了解决上面的问题，需要将新添加的方法，改成默认方法。public省略不写，也会默认为public</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title">methodDefault</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		<span class="comment">//一定要将这个大括号写上</span></span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是新添加的默认方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceDefaultA.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceDefaultA</span> <span class="keyword">implements</span> <span class="title">MyInterfaceDefault</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"实现了抽象方法，AA"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceDefaultB.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceDefaultB</span> <span class="keyword">implements</span> <span class="title">MyInterfaceDefault</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"实现了抽象方法，BB"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodDefault</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"实现类B覆盖重写了接口的默认方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo02Interface.java</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> void main(<span class="keyword">String</span>[] args) &#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//创建实现类对象</span></span><br><span class="line">		MyInterfaceDefaultA a=<span class="keyword">new</span> <span class="type">MyInterfaceDefaultA</span>();</span><br><span class="line">		a.methodAbs();<span class="comment">//调用抽象方法，实际运行的是右侧实现类</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//调用默认方法，如果实现类中没有，会向上寻找</span></span><br><span class="line">		a.methodDefault();<span class="comment">//这是新添加的默认方法</span></span><br><span class="line">		</span><br><span class="line">		MyInterfaceDefaultB b=<span class="keyword">new</span> <span class="type">MyInterfaceDefaultB</span>();</span><br><span class="line">		b.methodAbs();</span><br><span class="line">		b.methodDefault();<span class="comment">//实现类B覆盖重写了默认方法</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、接口的静态方法"><a href="#五、接口的静态方法" class="headerlink" title="五、接口的静态方法"></a>五、接口的静态方法</h2><p>从Java 8开始，接口中允许定义静态方法。</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> 返回值类型 方法名称(参数列表)&#123;</span><br><span class="line"><span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>提示：</p>
<p>就是将abstract或者default换成static，然后带上方法体。</p>
<p>注意：不能通过接口实现类的对象来调用接口当中的静态方法。</p>
<p>正确用法：直接通过接口名称调用静态方法</p>
<p>格式：</p>
<p>接口名称.静态方法(参数列表)</p>
<p>MyInterfaceStatic.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">MyInterfaceStatic</span> &#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//public 可以省略</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">methodStatic</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是接口的静态方法！"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceStaticImpl.java</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceStaticImpl</span> <span class="keyword">implements</span> <span class="title">MyInterfaceStatic</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo03Interface.java</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo03Interface</span> &#123;</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"><span class="comment">//		MyInterfaceStaticImpl impl=new MyInterfaceStaticImpl();</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//错误写法！</span></span><br><span class="line">		<span class="comment">//impl.methodStatic();</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//直接通过接口名称调用静态方法</span></span><br><span class="line">		MyInterfaceStatic.methodStatic();</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="六、接口的私有方法"><a href="#六、接口的私有方法" class="headerlink" title="六、接口的私有方法"></a>六、接口的私有方法</h2><p>问题描述：</p>
<p><em>我们需要抽取一个公共方法，用来解决两个默认方法之间的重复代码的问题，但是这个共有方法不应该让实现类使用，应该是私有化的。</em></p>
<p>解决方案：</p>
<p>从Java 9开始，接口里允许定义私有方法</p>
<ol>
<li><p>普通私有方法，解决多个默认方法之间重复代码问题</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> 返回值类型 方法名称(参数列表)&#123;</span><br><span class="line"><span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>静态私有方法，解决多个静态方法之间的重复代码问题<br>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> 返回值类型 方法名称(参数列表)&#123;</span><br><span class="line"><span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>MyInterfacePrivateA.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">MyInterfacePrivateA</span> &#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title">methodDefault1</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		 System.<span class="keyword">out</span>.println(<span class="string">"这是默认方法1"</span>);</span><br><span class="line"><span class="comment">//		 System.out.println("AAA");</span></span><br><span class="line"><span class="comment">//		 System.out.println("BBB");</span></span><br><span class="line"><span class="comment">//		 System.out.println("CCC");</span></span><br><span class="line">		 <span class="comment">//methodCommon();</span></span><br><span class="line">		 </span><br><span class="line">		 methodPrivateCommon();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title">methodDefault2</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是默认方法2"</span>);</span><br><span class="line"><span class="comment">//		System.out.println("AAA");</span></span><br><span class="line"><span class="comment">//		System.out.println("BBB");</span></span><br><span class="line"><span class="comment">//		System.out.println("CCC");</span></span><br><span class="line">		<span class="comment">//methodCommon();</span></span><br><span class="line">		</span><br><span class="line">		methodPrivateCommon();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//如果就像上面的一样，有重复的咋办？</span></span><br><span class="line">	<span class="comment">//像下面这样，写个方法呗？但是...</span></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title">methodCommon</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"AAA"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"BBB"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"CCC"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//用下面这个就完美解决了</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">methodPrivateCommon</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"AAA"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"BBB"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"CCC"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfacePrivateB.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title">MyInterfacePrivateB</span> &#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">methodStatic1</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		 System.<span class="keyword">out</span>.println(<span class="string">"这是静态方法1"</span>);</span><br><span class="line">		 </span><br><span class="line">		 methodPrivateCommon();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">methodStatic2</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"这是静态方法2"</span>);</span><br><span class="line">		</span><br><span class="line">		methodPrivateCommon();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">methodPrivateCommon</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"AAA"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"BBB"</span>);</span><br><span class="line">		System.<span class="keyword">out</span>.println(<span class="string">"CCC"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfacePrivateAImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfacePrivateAImpl</span> <span class="keyword">implements</span> <span class="title">MyInterfacePrivateA</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAnother</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//methodCommon();//直接访问接口中的默认方法，但是这个不应该被访问到啊</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="七、接口的常量定义以及使用"><a href="#七、接口的常量定义以及使用" class="headerlink" title="七、接口的常量定义以及使用"></a>七、接口的常量定义以及使用</h2><p>接口当中，也可以定义”成员变量”</p>
<p>但是这个成员变量，需要public static final三个关键字进行修饰</p>
<p>从效果上看，这就是接口的常量</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> 数据类型 常量名称 = 数据值;</span><br></pre></td></tr></table></figure>

<p>一旦使用final关键字进行修饰，说明不可改变</p>
<ol>
<li>接口当中的常量，可以省略这三个关键字，但即使不写，还是默认为自带这三个关键字</li>
<li>接口当中的常量，必须进行赋值</li>
<li>接口当中的常量名称，使用完全大写，并且下划线隔开</li>
</ol>
<p>MyInterfaceConst.java</p>
<figure class="highlight axapta"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">MyInterfaceConst</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//这其实就是一个常量，一旦赋值，是不可以修改的</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> NUM_OF_MY_CLASS =<span class="number">10</span>;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo05Interface.java</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Demo05Interface</span> &#123;</span><br><span class="line">	<span class="comment">//访问接口当中的常量</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(MyInterfaceConst.NUM_OF_MY_CLASS);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="八、接口内容总结"><a href="#八、接口内容总结" class="headerlink" title="八、接口内容总结"></a>八、接口内容总结</h2><p>在Java 9+版本中（带有中括号表示可以省略）</p>
<ol>
<li><p>成员变量其实是常量，格式：</p>
<figure class="highlight accesslog"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">[public]</span> <span class="string">[static]</span> <span class="string">[final]</span> 数据类型 常量名称 = 数据值;</span><br></pre></td></tr></table></figure>

<p>注意:</p>
<pre><code>常量必须进行赋值，而且一旦赋值不能改变。
常量名称完全大写，用下划线进行分割。</code></pre></li>
</ol>
<ol start="2">
<li><p>接口中最重要的就是抽象方法，格式：</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="keyword">public</span>] [<span class="class"><span class="keyword">abstract</span>] 返回值类型 方法名称(<span class="type">参数列表</span>);</span></span><br></pre></td></tr></table></figure>

<p>注意：实现类必须覆盖重写接口中所有的抽象方法，除非实现类是抽象类。</p>
</li>
<li><p>从java 8开始，接口里允许定义默认方法，格式：</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[public]<span class="built_in"> default </span>返回值类型 方法名称(参数列表) &#123; 方法体 &#125;</span><br></pre></td></tr></table></figure>

<p>注意：默认方法也可以被覆盖重写。</p>
</li>
<li><p>从java 8开始，接口里允许定义静态方法，格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="keyword">public</span>] <span class="keyword">static</span> 返回值类型 方法名称(参数列表) &#123; 方法体 &#125;</span><br></pre></td></tr></table></figure>

<p>注意：应该通过接口名称进行调用，不能通过实现类对象调用接口静态方法</p>
</li>
<li><p>从java 9开始，接口里允许定义私有方法，格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">普通私有方法：<span class="keyword">private</span> 返回值类型 方法名称(参数列表) &#123; 方法体 &#125;</span><br><span class="line">静态私有方法：<span class="keyword">private</span> <span class="keyword">static</span> 返回值类型 方法名称(参数列表) &#123; 方法体 &#125;</span><br></pre></td></tr></table></figure>

<p>注意：private的方法只有接口自己才能调用，不能被实现类或者别人使用。</p>
</li>
</ol>
<h2 id="九、使用接口时的注意事项"><a href="#九、使用接口时的注意事项" class="headerlink" title="九、使用接口时的注意事项"></a>九、使用接口时的注意事项</h2><h4 id="使用接口的时候，需要注意："><a href="#使用接口的时候，需要注意：" class="headerlink" title="使用接口的时候，需要注意："></a>使用接口的时候，需要注意：</h4><ol>
<li><p>接口是没有静态代码块或者构造方法的</p>
</li>
<li><p>一个类的直接父类是唯一的，但是一个类可以同时实现多个接口<br>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceImpl</span> <span class="keyword">implements</span> <span class="title">MyInterfaceA</span>,<span class="title">MyInterfaceB</span></span>&#123;</span><br><span class="line"><span class="comment">//覆盖重写所有抽象方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果实现类所实现的多个接口当中，存在重复的抽象方法，那么只需要覆盖重写一次即可</p>
</li>
<li><p>如果实现类没有覆盖重写所有接口当中的所有抽象方法，那么实现类就必须是抽象类</p>
</li>
<li><p>如果实现类实现的多个接口当中，存在重复的默认方法，那么实现类一定要对冲突的默认方法进行覆盖重写</p>
</li>
<li><p>一个类，如果直接父类当中的方法，跟接口当中继承的方法产生了冲突，优先用父类当中的方法。</p>
</li>
</ol>
<p>MyInterfaceA.java</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public<span class="built_in"> interface </span>MyInterfaceA &#123;</span><br><span class="line">	public abstract void methodA();</span><br><span class="line">	</span><br><span class="line">	public abstract void methodAbs();</span><br><span class="line">	</span><br><span class="line">	public<span class="built_in"> default </span>void methodDefault() &#123;</span><br><span class="line">		System.out.println(<span class="string">"默认方法AAA"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceB.java</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">public<span class="built_in"> interface </span>MyInterfaceB &#123;</span><br><span class="line">	public abstract void methodB();</span><br><span class="line">	public abstract void methodAbs();</span><br><span class="line">	</span><br><span class="line">	public<span class="built_in"> default </span>void methodDefault() &#123;</span><br><span class="line">		System.out.println(<span class="string">"默认方法BBB"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceAbstract.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceAbstract</span> <span class="keyword">implements</span> <span class="title">MyInterfaceA</span>,<span class="title">MyInterfaceB</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodDefault</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"不管抽不抽象，有冲突，都得重写"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfacImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceImpl</span> <span class="keyword">implements</span> <span class="title">MyInterfaceA</span>,<span class="title">MyInterfaceB</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodB</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"覆盖重写A接口抽象方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodA</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"覆盖重写B接口抽象方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"覆盖重写了AB接口都有的抽象方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodDefault</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		System.out.println(<span class="string">"对多个接口当中的冲突的默认方法进行覆盖重写"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo01Interface.java</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01Interface</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="接口之间的多继承"><a href="#接口之间的多继承" class="headerlink" title="接口之间的多继承"></a>接口之间的多继承</h4><ol>
<li>类与类之间是单继承的。直接父类只能有一个。</li>
<li>类与接口直接是多实现的。一个类可以实现多个接口，有冲突需要解决。</li>
<li>接口与接口之间是多继承的。</li>
</ol>
<p>注意事项：</p>
<ol>
<li>多个父接口当中的抽象方法如果重复，没关系</li>
<li>多个父接口当中的默认方法如果重复，那么子接口必须进行默认方法的覆盖重写，而且带着default关键字。</li>
</ol>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>